/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsGroupDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IGoodsGroupAttrDao;
import com.xteam.ecsystem.entities.BaseGroupAttribute;

/**
 * 商品分类属性数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class GoodsGroupAttrDaoImpl extends DDLPrefix implements IGoodsGroupAttrDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	/**
	 * 
	 * 新增或者修改商品分类属性信息
	 * @author liuzhi
	 * @param obj
	 * @param id
	 */
	public void saveOrUpdateObject(String bgaGgroup, String bgaGtype,
			String bgaTattri, String gBggId){
		String[] btaIds = null;
		BaseGroupAttribute bgAttr = null;
		
		try {
			if (gBggId != null && gBggId != "" && !"null".equals(gBggId)
					&& !"undefined".equals(gBggId)) {
				delBggAttrByBgaGtype(gBggId);
			}

			if (bgaTattri != null && !"".equals(bgaTattri) 
					&& !"null".equals(bgaTattri) 
					&& !"undefined".equals(bgaTattri)) {
				btaIds = bgaTattri.split(",");
				for (String btaId : btaIds) {
					bgAttr = new BaseGroupAttribute();
					bgAttr.setBgaGgroup(bgaGgroup);
					bgAttr.setBgaGtype(bgaGtype);
					bgAttr.setBgaTattri(btaId);
					bgAttr.setBgaOrder(50);

					this.baseDao.save(bgAttr);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据商品分类属性编号获得对应的商品分类属性信息
	 * 
	 * @author liuzhi
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getBggAttrByBgaGgroup(String bgaGgroup){
		JSONArray array = new JSONArray();
		List list = new ArrayList();
		try {
			String hql = SQL_FROM + BaseGroupAttribute.REF + SQL_WHERE +" 1=1 " 
				+ SQL_AND + " bgaGgroup=?";
			list = this.baseDao.find(hql, new Object[]{bgaGgroup});
			
			array = JSONArray.fromObject(list);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return array.toString();
	}

	public void delBggAttrByBgaGtype(String gBggId) {
		String hql = "";
		try {
			hql = SQL_DELETE + SQL_FROM + BaseGroupAttribute.REF + SQL_WHERE + " bgaGgroup=? ";
			this.baseDao.executeUpdate(hql, new Object[]{gBggId});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
